<?php

namespace Lib\Socket;

class Client {

	/**
	 * 扫描端口
	 *
	 * @param 主机 $sHost        	
	 * @param 开始端口号 $iMin        	
	 * @param 结束端口号 $iMax        	
	 * @param 接口协议 $sProtocol
	 *        	all：tcp和upd
	 *        	tcp：tcp
	 *        	udp：udp
	 * @return array
	 */
	public function scanPort($sHost, $iMin, $iMax, $sProtocol = 'tcp') {
		global $o;
		if ($sProtocol == 'tcp' || $sProtocol == 'all') {
			for($i = $iMin; $i <= $iMax; $i ++) {
				$rStock = @fsockopen ( $sHost, $i, $aError ['code'], $aError ['message'], 1 );
				if ($aError ['code'] == 0) {
					$aPort ['udp'] [] = $i;
					fclose ( $rStock );
				} elseif ($aError ['code'] == 64) {
					$o->Socket->setError ( $aError ['code'], $aError ['message'] );
					return false;
				} else {
					$o->Socket->setError ( $aError ['code'], $aError ['message'] );
				}
			}
		}
		if ($sProtocol == 'udp' || $sProtocol == 'all') {
			for($i = $iMin; $i <= $iMax; $i ++) {
				$rStock = @fsockopen ( 'udp://' . $sHost, $i, $aError ['code'], $aError ['message'], 1 );
				if ($aError ['code'] == 0) {
					$aPort ['udp'] [] = $i;
					fclose ( $rStock );
				} elseif ($aError ['code'] == 64) {
					$o->Socket->setError ( $aError ['code'], $aError ['message'] );
					return false;
				} else {
					$o->Socket->setError ( $aError ['code'], $aError ['message'] );
				}
			}
		}
		if ($sProtocol == 'tcp') {
			return $aPort ['tcp'];
		} elseif ($sProtocol == 'udp') {
			return $aPort ['udp'];
		} else {
			return $aPort;
		}
	}
}

?>